#include "listwidget.h"
#include "ui_listwidget.h"

#include <QMessageBox>

ListWidget::ListWidget(const QVector<Character>& list, QWidget *parent)
    : QDockWidget(parent)
    , ui(new Ui::ListWidget)
    , charList(list)
{
    ui->setupUi(this);

    connect(ui->pushButton_exit,&QPushButton::clicked,this,[=](){
        this->close();
        emit CloseWidget();
    });

    setWindowTitle("字符表");
    SetList(charList);
    // 设置只读
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->pushButton_search,&QPushButton::clicked,this,[=](){
        auto str = ui->lineEdit_search->text();
        if(str.size() > 1) {
            QMessageBox::critical(nullptr,"错误","请输入单个字符后查询");
        } else {
            auto it = std::find(charList.begin(),charList.end(),str[0]);
            if(it == charList.end()) {
                QMessageBox::critical(nullptr,"错误",QString("%1不在当前字符表中").arg(str[0]));
            } else {
                // 滚动到指定行
                ui->tableWidget->scrollToItem(ui->tableWidget->item(it - charList.begin(), 0), QAbstractItemView::PositionAtTop);
                ui->tableWidget->setCurrentItem(ui->tableWidget->item(it - charList.begin(),0));

                QMessageBox::information(nullptr,"提示",QString("已找到当前字符: \nid: %1\n类型: %2").arg(it - charList.begin() + 1).arg(it->GetVarietyString_cn()));
            }
        }
    });

}
ListWidget::ListWidget(const QVector<Word>& list, QWidget *parent)
    : QDockWidget(parent)
    , ui(new Ui::ListWidget)
    , wordList(list)
{
    ui->setupUi(this);

    connect(ui->pushButton_exit,&QPushButton::clicked,this,[=](){
        this->close();
        emit CloseWidget();
    });

    setWindowTitle(QString("%1表").arg(wordList[0].GetVarietyString_cn()));
    SetList(wordList);
    // 设置只读
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->pushButton_search,&QPushButton::clicked,this,[=](){
        auto str = ui->lineEdit_search->text();
        auto it = std::find(wordList.begin(),wordList.end(),str);
        if(it == wordList.end()) {
            QMessageBox::critical(nullptr,"错误",QString("%1不在当前%2表中").arg(str).arg(wordList[0].GetVarietyString_cn()));
        } else {
            // 滚动到指定行
            ui->tableWidget->scrollToItem(ui->tableWidget->item(it - wordList.begin(), 0), QAbstractItemView::PositionAtTop);
            ui->tableWidget->setCurrentItem(ui->tableWidget->item(it - wordList.begin(),0));

            QMessageBox::information(nullptr,"提示",QString("已找到当前单词: \nid: %1\n类型: %2").arg(it - wordList.begin() + 1).arg(it->GetVarietyString_cn()));
        }
    });
}

ListWidget::~ListWidget()
{
    delete ui;
}

void ListWidget::closeEvent(QCloseEvent * event) {
    emit CloseWidget();
    event->accept();
}

template<typename T>
void ListWidget::SetList(const QVector<T> &list) {
    for(int row = 0;row < list.size();row++) {
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row,0,new QTableWidgetItem(QString(list[row].GetContent())));
        ui->tableWidget->setItem(row,1,new QTableWidgetItem(list[row].GetVarietyString_cn()));
    }
}
